﻿<SQLStoredProcedureSpec
	xmlns="bb_appfx_sqlstoredprocedure"
	xmlns:common="bb_appfx_commontypes" 
	ID="9e44f13b-0cf9-43f7-869a-0402e67ed741"
	Name="Constituent Language Attribute Add"
	Description="Add a language to the language attributes of a constituent"
	Author="Blackbaud Professional Services"
	SPName="USR_USP_ADD_CONSTITUENT_LANGUAGE"
	>

	<CreateProcedureSQL>
		<![CDATA[
create procedure dbo.USR_USP_ADD_CONSTITUENT_LANGUAGE
(
  @ConstituentID uniqueidentifier
 ,@USR_ATTR_LANGUAGECODEID uniqueidentifier
 ,@changeagentid uniqueidentifier = null
)
as
begin
  --given a constituent and a language, add that language as an attribute to the constituent
  if @changeagentid is null begin
    exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;
  end;

  declare @LanguageAttributeTableName nvarchar(100) = dbo.USR_UFN_GETATTRIBUTETABLENAME('USR_ATTR_LANGUAGECODE','Language');
  declare @str_constituentid nvarchar(100) = convert(nvarchar(100),@constituentid);
  declare @str_usr_attr_languagecodeid nvarchar(100) = convert(nvarchar(100),@USR_ATTR_LANGUAGECODEID);
  declare @str_changeagentid nvarchar(100) = convert(nvarchar(100),@changeagentid);
  declare @sqltorun nvarchar(max) = null;  
  
  set @sqltorun = 'exec dbo.USP_' + @LanguageAttributeTableName + '_ADD null, ''' + @str_constituentid + ''',''' + @str_usr_attr_languagecodeid + ''', '''', ''' + @str_changeagentid + ''', null, null;';
  exec(@sqltorun);
end;

]]>
	</CreateProcedureSQL>
</SQLStoredProcedureSpec>